<template>
	<view>
		<!-- 自定义导航 -->
		<tn-nav-bar alpha />
		<!-- 加载动画 -->
		<u-loading-page :loading="loading" />
		<!-- 提示信息 -->
		<u-toast ref="uToast" />
		<!-- 选择学校 -->
		<tn-select v-model="showShool" title="选择学校" mode="single" :list="schoolList" :maskCloseable="false" safeAreaInsetBottom labelName="name" valueName="id" @confirm="confirmSelect" />
		<view class="main-box">
			<view class="bg-one" />
			<view class="bg-two" />
			<view class="tips">
				<text class="title">学生认证</text>
				<text class="mt-2 subtitle text-grey">账户密码与教务系统一致~</text>
			</view>
			<form @submit="login">
				<view class="form-box">
					<view class="input-box" @click="select">
						<view class="icon p-3">
							<u-icon name="arrow-down-fill" size="20" color="#37c0fe" />
						</view>
						<input type="text" disabled placeholder="请选择学校" v-model="school.label" />
						<input hidden name="id" v-model="school.value" />
					</view>
					<view class="input-box">
						<view class="p-3">
							<u-icon name="account" size="22" color="#37c0fe" />
						</view>
						<input type="number" name="username" placeholder="请输入学号" maxlength="15" value="" />
					</view>
					<view class="input-box">
						<view class="p-3">
							<u-icon name="lock" size="22" color="#37c0fe" />
						</view>
						<input :password="!showpass" name="password" placeholder="请输入密码" maxlength="20" value="" />
						<view class="p-4">
							<u-icon :name="showpass ? 'eye-fill' : 'eye-off'" size="22" :color="showpass ? '#37c0fe' : '#999'" @click="showPass" />
						</view>
					</view>
					<button v-if="showBtn" form-type="submit">登录</button>
				</view>
			</form>
		</view>
	</view>
</template>

<script>
import { getSchoolList, auth } from '@/api'
export default {
	data() {
		return {
			loading: true, // 加载动画
			showShool: false,	// 选择学校
			showpass: false, // 显示密码
			showBtn: true, // 禁用按钮
			schoolList: [], // 学校列表
			school: null,	// 学校
		};
	},
	onLoad() {
		// 获取学校列表
		this.getSchoolList();
	},

	methods: {
		// 学校列表
		async getSchoolList() {
			const { code, data, message } = await getSchoolList();
			if (code !== 1000) {
				return this.$refs.uToast.show({
					type: 'error',
					message: message
				});
			}
			this.schoolList = data;
			this.loading = false;
		},

		// 登录
		async login(e) {
			const { id, username, password } = e.detail.value;
			if (uni.$u.test.isEmpty(id)) {
				return this.$refs.uToast.show({
					type: 'error',
					message: '请选择学校'
				});
			}
			if (uni.$u.test.isEmpty(username)) {
				return this.$refs.uToast.show({
					type: 'error',
					message: '请输入学号'
				});
			}
			if (!uni.$u.test.digits(username)) {
				return this.$refs.uToast.show({
					type: 'error',
					message: '学号必须为数字'
				});
			}
			if (uni.$u.test.isEmpty(password)) {
				return this.$refs.uToast.show({
					type: 'error',
					message: '请输入密码'
				});
			}
			uni.showLoading({ title: '登录中', mask: true });
			this.showBtn = false;
			const { code, data, message } = await auth({ id, username, password });
			uni.hideLoading();
			if (code !== 1000) {
				this.showBtn = true;
				return this.$refs.uToast.show({
					type: 'error',
					message: message
				});
			}
			this.showBtn = true;
			return this.$refs.uToast.show({
				type: 'success',
				message: '登录成功',
				complete() {
					uni.navigateBack();
				}
			});
		},

		// 显示密码
		showPass() {
			this.showpass = !this.showpass;
		},

		// 选择学校
		select() {
			this.showShool = true;
		},

		// 确认选择学校
		confirmSelect(e) {
			this.showShool = false;
			this.school = e[0];
		},
	}
}
</script>

<style lang="scss">
.main-box {
	width: 100vw;
	height: 100vh;
	margin: 0;
	padding: 0;
	position: relative;
	background-color: rgb(118, 218, 255);
	overflow: hidden;

	&::after {
		content: '';
		position: absolute;
		bottom: 15vh;
		min-width: 300vw;
		min-height: 300vw;
		left: -95%;
		border-radius: 46%;
		background-color: #FFFFFF;
		-webkit-animation: rote 10s linear infinite;
		animation: rote 10s linear infinite;
	}

	&::before {
		content: '';
		position: absolute;
		bottom: 15vh;
		min-width: 300vw;
		min-height: 300vw;
		left: -95%;
		border-radius: 45%;
		opacity: .5;
		background-color: #FFFFFF;
		-webkit-animation: rote 10s linear infinite;
		animation: rote 10s linear infinite;
	}

	.bg-one {
		position: fixed;
		top: -120rpx;
		right: -230rpx;
		width: 600rpx;
		height: 600rpx;
		border-radius: 100%;
		background-color: #00baef;
		z-index: 2;
	}

	.bg-two {
		position: fixed;
		top: -0rpx;
		right: -300rpx;
		width: 600rpx;
		height: 600rpx;
		border-radius: 100%;
		background-color: #ade8f9;
		z-index: 1;
	}

	.tips {
		position: relative;
		padding-top: 200rpx;
		padding-left: 80rpx;
		display: flex;
		flex-direction: column;
		z-index: 2;

		.title {
			line-height: 70rpx;
			font-weight: bold;
			font-size: 50rpx;
		}

		.subtitle {
			line-height: 70rpx;
			font-size: 35rpx;
			font-weight: bold;
			color: #b0b0b1;
		}
	}

	.form-box {
		padding: 60rpx;
		position: relative;
		z-index: 9;

		.icon {
			font-size: 65rpx;
		}

		.name {
			margin-top: -20rpx;
		}

		.input-box {
			margin: 40rpx 0;
			display: flex;
			justify-content: flex-start;
			align-items: center;
			height: 100rpx;
			width: 100%;
			background-color: #f5f5f5;
			border-radius: 100rpx;

			text {
				font-size: 36rpx;
			}

			input {
				flex: 1;
				height: 100%;
				font-size: 30rpx;
			}
		}

		button {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 100rpx !important;
			border-radius: 100rpx;
			color: #FFFFFF;
			background: linear-gradient(to right, #00c6fc, #9adcf1);

			&::after {
				border: 0 !important;
			}
		}
	}
}

@-webkit-keyframes rote {
	from {
		-webkit-transform: rotateZ(0);
		transform: rotateZ(0);
	}

	to {
		-webkit-transform: rotateZ(360deg);
		transform: rotateZ(360deg);
	}
}

@keyframes rote {
	from {
		-webkit-transform: rotateZ(0);
		transform: rotateZ(0);
	}

	to {
		-webkit-transform: rotateZ(360deg);
		transform: rotateZ(360deg);
	}
}
</style>
